import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router';
import { nextTick } from 'vue';
import store from '../store';
// const { changeThemeColor } = require('../utils/theme');
const Index = () => import('@/layout/indexs.vue');
const Layout = () => import('@/layout/index.vue');
const Users = () => import('@/views/accommodation/studentInfo');
const Teacher = () => import('@/views/accommodation/teacherInfo');
const Class = () => import('@/views/accommodation/studentClass');
const classDistributionDeta = () => import('@/views/accommodation/studentClass/distributionDeta');
const info = () => import('@/views/accommodation/studentClass/classInfo');
const classInfoDeta = () => import('@/views/accommodation/studentClass/classInfoDeta');
const Room = () => import('@/views/accommodation/roomManage');
const Dormitory = () => import('@/views/accommodation/dormitoryManage');
const DormitoryPersonal = () => import('@/views/assessment/dormitoryPersonal');
const PersonalInteriorDeta = () => import('@/views/assessment/dormitoryPersonal/interiorDeta');
const PersonalDisciplineDeta = () => import('@/views/assessment/dormitoryPersonal/disciplineDeta');
const PersonalAllDeta = () => import('@/views/assessment/dormitoryPersonal/allDeta');

const DormitoryCollective = () => import('@/views/assessment/dormitoryCollective');
const CollectiveInteriorDeta = () => import('@/views/assessment/dormitoryCollective/interiorDeta');
const CollectiveDisciplineDeta = () =>
  import('@/views/assessment/dormitoryCollective/disciplineDeta');
const CollectiveAllDeta = () => import('@/views/assessment/dormitoryCollective/allDeta');

const ClassCollective = () => import('@/views/assessment/classCollective');
const ClassInteriorDeta = () => import('@/views/assessment/classCollective/interiorDeta');
const ClassInteriorDetaDeduct = () =>
  import('@/views/assessment/classCollective/interiorDeta/deduct');
const ClassDisciplineDeta = () => import('@/views/assessment/classCollective/disciplineDeta');
const ClassAllDeta = () => import('@/views/assessment/classCollective/allDeta');
const Inspect = () => import('@/views/assessment/inspect');
const InspectDeta = () => import('@/views/assessment/inspect/details');
const LeaveInquiry = () => import('@/views/leaveInquiry');
const RuleSetting = () => import('@/views/ruleSetting');
const RuleSettingChange = () => import('@/views/ruleSettingChange');
const RuleSettingLamp = () => import('@/views/ruleSettingLamp');
// const Extern = () => import('@/views/extern');
const Extern = () => import('@/views/extern/extern');
const Error = () => import('@/views/error');
const Jurisdiction = () => import('@/views/jurisdiction');
const routes = [
  {
    path: '/',
    name: '首页',
    component: Layout,
    redirect: '/admin/usersInfo',
    children: [
      {
        path: '/admin', // 住宿
        redirect: '/admin/usersInfo',
        component: () => import('@/views/admin'),
        name: '住宿管理',
        children: [
          {
            path: '/admin/usersInfo', // 学生信息管理
            component: Users,
            name: '学生信息管理'
          },
          {
            path: 'teacherInfo', // 学生信息管理
            component: Teacher,
            name: '教职工信息管理'
          },
          {
            path: 'classInfo', // 班级信息管理
            component: Class,
            name: '班级信息管理'
          },
          {
            path: 'dormitoryManage', // 宿舍楼管理
            component: Dormitory,
            name: '宿舍楼管理'
          },
          {
            path: 'roomManage', // 房间管理
            component: Room,
            name: '房间管理'
          }
        ]
      },
      {
        path: 'check', // 住宿
        redirect: '/check/dormitoryPersonal',
        component: () => import('@/views/check'),
        name: '考核查询',
        children: [
          {
            path: 'dormitoryPersonal', // 宿舍个人考核
            component: DormitoryPersonal,
            name: '宿舍个人考核'
          },
          {
            path: 'dormitoryCollective', // 宿舍集体考核
            component: DormitoryCollective,
            name: '宿舍集体考核'
          },
          {
            path: 'classCollective', // 班级集体考核
            component: ClassCollective,
            name: '班级集体考核'
          },
          {
            path: 'inspect', // 检查
            component: Inspect,
            name: '检查记录'
          }
        ]
      },
      {
        path: 'leaveInquiry', // 请假查询
        component: LeaveInquiry,
        name: '请假查询'
      },
      {
        path: 'rule', // 规则
        redirect: '/rule/ruleSetting',
        component: () => import('@/views/rule'),
        name: '规则设置',
        children: [
          {
            path: 'ruleSetting', // 考核规则设置 ------
            component: RuleSetting,
            name: '规则设置'
          },
          {
            path: 'ruleSettingLamp', // 亮灯规则设置
            component: RuleSettingLamp,
            name: '亮灯规则设置'
          }
        ]
      },
      {
        path: 'ruleSetting', // 考核规则设置 ------
        component: RuleSetting,
        name: '规则设置'
      },
      {
        path: 'ruleSettingChange', // 新规则设置
        component: RuleSettingChange,
        name: '新规则设置'
      },
      {
        path: 'ruleSettingLamp', // 亮灯规则设置
        component: RuleSettingLamp,
        name: '亮灯规则设置'
      },
      {
        path: 'extern', // 走读管理
        component: Extern,
        name: '走读管理'
      },
      {
        path: 'jurisdiction', // 权限管理
        component: Jurisdiction,
        name: '权限管理'
      }
    ]
  },
  {
    path: '/check/dormitoryPersonal/interiorDeta', // 宿舍个人考核-内务检查详情
    component: PersonalInteriorDeta,
    name: '宿舍个人考核-详情',
    meta: {
      isShow: true
    }
  },
  {
    path: '/admin/classInfo/distributionDeta', // 班级信息管理-宿舍分配
    component: classDistributionDeta,
    name: '宿舍分配',
    meta: {
      isShow: true
    }
  },
  {
    path: '/check/dormitoryPersonal/disciplineDeta', // 宿舍个人考核-纪律检查详情
    component: PersonalDisciplineDeta,
    name: '宿舍个人考核-详情',
    meta: {
      isShow: true
    }
  },
  {
    path: '/check/dormitoryPersonal/allDeta', // 宿舍个人考核-综合检查详情
    component: PersonalAllDeta,
    name: '宿舍个人考核-综合查询详情',
    meta: {
      isShow: true
    }
  },
  {
    path: '/admin/classInfo/info', // 班级信息管理-班级详情
    component: info,
    name: '班级详情',
    meta: {
      isShow: true
    }
  },
  {
    path: '/admin/classInfo/classInfoDeta', // 班级信息管理-查看 精确分配
    component: classInfoDeta,
    name: '精确分配',
    meta: {
      isShow: true
    }
  },
  {
    path: '/check/dormitoryCollective/interiorDeta', // 宿舍集体考核-内务检查详情
    component: CollectiveInteriorDeta,
    name: '宿舍集体考核-详情',
    meta: {
      isShow: true
    }
  },
  {
    path: '/check/dormitoryCollective/disciplineDeta', // 宿舍集体考核-纪律检查详情
    component: CollectiveDisciplineDeta,
    name: '宿舍集体考核-详情',
    meta: {
      isShow: true
    }
  },
  {
    path: '/check/dormitoryCollective/allDeta', // 宿舍集体考核-综合检查详情
    component: CollectiveAllDeta,
    name: '宿舍集体考核-综合查询详情',
    meta: {
      isShow: true
    }
  },
  {
    path: '/check/classCollective/interiorDeta', // 班级集体考核-内务检查详情
    component: ClassInteriorDeta,
    name: '班级集体考核-详情',
    meta: {
      isShow: true
    }
  },
  {
    path: '/classCollective/interiorDeta/deduct', // 班级集体考核-内务检查详情-扣分详情
    component: ClassInteriorDetaDeduct,
    name: '班级集体考核-详情-扣分详情',
    meta: {
      isShow: true
    }
  },
  {
    path: '/check/classCollective/disciplineDeta', // 班级集体考核-纪律检查详情
    component: ClassDisciplineDeta,
    name: '班级集体考核-详情2',
    meta: {
      isShow: true
    }
  },
  {
    path: '/check/classCollective/allDeta', // 班级集体考核-综合检查详情
    component: ClassAllDeta,
    name: '班级集体考核-综合查询详情',
    meta: {
      isShow: true
    }
  },
  {
    path: '/inspect/details', // 检查记录-详情
    component: InspectDeta,
    name: '检查记录-详情',
    meta: {
      isShow: true
    }
  },
  {
    path: '/error', // 没权限
    component: Error,
    meta: {
      isShow: true
    }
  }
];

export function setupRouter() {
  // const base = window.__MICRO_APP_ENVIRONMENT__ ? window.__MICRO_APP_BASE_ROUTE__ : './';
  const base = window.__MICRO_APP_BASE_ROUTE__ || process.env.BASE_URL;
  const history = createWebHistory(base);
  const router = createRouter({
    history,
    routes,
    scrollBehavior(to, from, savedPosition) {
      // 始终滚动到顶部
      return { top: 0 };
    }
  });

  if (window.__MICRO_APP_ENVIRONMENT__) {
    router.beforeEach(() => {
      if (window.history.state?.current) {
        window.history.state.current = '/';
      }
    });
    router.afterEach(() => {
      if (typeof window.history.state === 'object') {
        const _baseRoute = window.__MICRO_APP_BASE_ROUTE__;
        window.history.state.current = `/${_baseRoute.slice(_baseRoute.indexOf('microapp'))}`;
      }
    });
  }
  return {
    history,
    router
  };
}
